package com.cskaoyan.wordmemorize.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cskaoyan.wordmemorize.dao.entity.UserPrivilegeUseStatisticsDO;


import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper // todo 我加的
public interface UserPrivilegeHistoryMapper extends BaseMapper<UserPrivilegeUseStatisticsDO> {

    // todo 我加的
    @Select({
            "SELECT id, user_id, privilege_code, use_count ",
            "FROM user_privilege_use_statistics",
            " WHERE user_id = #{userId} AND privilege_code = #{privilegeCode}"
    })
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "privilege_code", property = "privilegeCode", jdbcType = JdbcType.VARCHAR)
    })
    UserPrivilegeUseStatisticsDO selectByUserIdAndPrivilegeCode(
            @Param("userId") Long userId,
            @Param("privilegeCode") String privilegeCode
    );

    @Update("UPDATE user_privilege_use_statistics SET use_count = use_count + 1 WHERE id = #{id}")
    int incrementUseCount(@Param("id") Long id);

    @Select("SELECT use_count FROM user_privilege_use_statistics WHERE id = #{id}")
    int selectUseCountById(@Param("id") Long id);
}
